/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import jaxb.TinType;
import jaxb.Tins;

/**
 *
 * @author Administrator
 */
public class Controller2 extends HttpServlet {
    private boolean firstDangTin = true;
    private String path;
    private File fileDangTin;
    private Tins tins = new Tins();
    private JAXBContext jaxbContext;
    private JAXBElement jaxbElement;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, JAXBException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String action = request.getParameter("btAction");
            if (action.equals("dangtin")) {
                if(firstDangTin){
                    path = getServletContext().getRealPath("/");
                    fileDangTin = new File(path + "dangtin.xml");

                    jaxbContext = JAXBContext.newInstance("jaxb");
                    Unmarshaller u = jaxbContext.createUnmarshaller();
//                    jaxbElement = (JAXBElement)u.unmarshal(fileDangTin);
//                    tins = (Tins)jaxbElement.getValue();
                    tins = (Tins)u.unmarshal(fileDangTin);
                    System.out.println(tins.getTin().get(0).getTen());
                    firstDangTin = false;
                }

                String ten = request.getParameter("txtTen");
                String gioiTinh = request.getParameter("txtGioiTinh");
                String namSinh = request.getParameter("txtNamSinh");
                String noiDi = request.getParameter("txtNoiDi");
                String ngayDi = request.getParameter("txtNgayDi");
                String mota = request.getParameter("txtMota");
                String hinh = request.getParameter("txtHinh");

                TinType tin = new TinType();
                tin.setTen(ten);
                tin.setGioitinh(gioiTinh);
                tin.setNamsinh(namSinh);
                tin.setNoisinh(noiDi);
                tin.setNgaydi(ngayDi);
                tin.setMota(mota);                
                tin.setHinh(hinh);
                               
                tins.getTin().add(tin);
                
//                jaxbContext = JAXBContext.newInstance(Tins.class);
//                Marshaller mar = jaxbContext.createMarshaller();
//                mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
//                mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//                mar.marshal(tins, fileDangTin);
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (JAXBException ex) {
            Logger.getLogger(Controller2.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (JAXBException ex) {
            Logger.getLogger(Controller2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
